const publicMethod = require("../../public/method")
var getCaipinTotalObj = function(arr,type){
    arr = arr || [];
    let cpArr = [];
    let cpObj = {}

    let filterArr = [];
    let nowMonth = publicMethod.Format(new Date(),"yyyy-MM-");
    arr.forEach(item=>{
        switch(type){
            case "total":{
                filterArr.push(item)
                break;
            }
            case "month":{
                if(item["shijian"].includes(nowMonth)){
                    filterArr.push(item)
                }
                break;
            }
            case "week":{
                if(item["shijian"].includes(nowMonth)){
                    if(publicMethod.getWeek(item["shijian"]) == publicMethod.getWeek(publicMethod.Format(new Date(),"yyyy-MM-dd"))){
                        filterArr.push(item)
                    }
                }
                break;
            }
            default:{

            }
        }
    })
    filterArr.forEach((item,index)=>{
        // if(index>arr.length - 10){
        if(index>=0){
            // console.log(index)
            let caipinArr = item["caipin"].split(",");
            // console.log(caipinArr)
            caipinArr.forEach(caipin =>{
                if(cpArr.includes(caipin)){
                    cpObj[caipin] += 1;
                }
                else{
                    cpObj[caipin] = 1;
                    cpArr.push(caipin);
                }
            })
        }
    })
    let returnArr = []
    for(let key in cpObj){
        returnArr.push({
            name:key,
            num:cpObj[key]
        })
    }
    function sortId(a,b){
        return b.num - a.num;
    }
    returnArr = returnArr.sort(sortId)
    let nameArr = [],numArr = [];
    returnArr.forEach(item=>{
        nameArr.push(item["name"])
        numArr.push(item["num"])
    })
    return {
        "nameArr":nameArr,
        "numArr":numArr
    }
}
var getCaipinMonthObj = function(arr){

    arr = arr || [];
    arr.forEach((item,index)=>{
        if(index>arr.length - 10){
            // console.log(index)
        }
    })
    return {}
}
var getCaipinWeekObj = function(arr){

    arr = arr || [];
    arr.forEach((item,index)=>{
        if(index>arr.length - 10){
            // console.log(index)
        }
    })
    return {}
}


module.exports = {getCaipinTotalObj,getCaipinMonthObj,getCaipinWeekObj}